package com.hctforgreen.greenservice;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.LoginResultEntity;
import com.hctforgreen.greenservice.model.ResultEntity;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.KeyboardUtil;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.hctforgreen.greenservice.utils.Utils;
import com.umeng.analytics.MobclickAgent;

/**
 * 添加反馈页面(软件设置页内)
 * 
 * @author Tallen Zhai
 * 
 */
public class AddFeedActivity extends ParentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_feed);
		initSkinLayout();
		initClickEdgehideKeyboard();
		initTitleButtonBar();
		initWindow();
	}

//	private void initSkinLayout() {
//		LinearLayout parentLyt = (LinearLayout) findViewById(R.id.lyt_parent);
//		parentLyt.setBackgroundResource(ThemeStoreUtil.getSkinId(this));
//	}

	/**
	 * 点击边缘隐藏输入法
	 */
	private void initClickEdgehideKeyboard() {
		LinearLayout rootLyt = (LinearLayout) findViewById(R.id.lyt_root);
		rootLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				KeyboardUtil keyboardUtil = new KeyboardUtil();
				keyboardUtil.hideSoftKeyboard(v);
			}
		});
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.add_feed_title_hint));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initWindow() {
		final EditText content = (EditText) findViewById(R.id.content);
		final Button submit = (Button) findViewById(R.id.submit);
		submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String str = content.getText().toString().trim();
				if ("".equals(str)) {
					Toast.makeText(AddFeedActivity.this,
							getString(R.string.add_feed_input_not_null_hint),
							Toast.LENGTH_SHORT).show();
					return;
				}

				submit(submit, str);
			}
		});
	}

	private void submit(final View submit, final String content) {
		submit.setClickable(false);
		final ProgressBar loadPb = (ProgressBar) findViewById(R.id.pb_loading);
		loadPb.setVisibility(View.VISIBLE);
		MobclickAgent.onEvent(AddFeedActivity.this, "Submit_setting_feadback");

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				submit.setClickable(true);
				loadPb.setVisibility(View.GONE);
				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					Utils.submitPoints(AddFeedActivity.this, 2, "", false);

					hctResult = (HctResult) msg.obj;
					ResultEntity entity = (ResultEntity) hctResult.data;
					if (entity.statusCode
							.equals(HctConstants.RESULT_STATE_SUCCESS)) {
						addFeedSuccessDiaolg();

					} else {
						Toast.makeText(AddFeedActivity.this,
								getString(R.string.net_error_hint),
								Toast.LENGTH_SHORT).show();
					}
					break;
				case HctConstants.STATUS_FAILD:
					Toast.makeText(AddFeedActivity.this,
							getString(R.string.net_error_hint),
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(
							AddFeedActivity.this);
					LoginResultEntity entity = LoginResultStoreUtil
							.get(AddFeedActivity.this);
					HctResult result = controller.submitReport(entity.personId,
							content);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 反馈成功对话框
	 */
	private void addFeedSuccessDiaolg() {
		try {
			/**
			 * 弹出dialog，不能脱离Activity，所以弄try catch
			 */
			LoginResultEntity resultEntity = LoginResultStoreUtil
					.get(AddFeedActivity.this);
			Dialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(
					AddFeedActivity.this);
			builder.setTitle(R.string.dialog_default_title_hint)
					.setMessage(
							getString(R.string.add_feed_success_dialog_content_header_hint)
									+ resultEntity.phone
									+ "（"
									+ resultEntity.personName
									+ "）"
									+ getString(R.string.add_feed_success_dialog_content_footer_hint))

					.setPositiveButton(R.string.dialog_confirm_hint,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									finish();
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
